<?php
namespace app\api\validate;


use app\api\model\Configs;

class UserScore extends Base
{
    protected $message = [
        'username.require' => '请输入用户名',
        'username.unique' => '该用户已存在',
        'username.length' => '用户名长度为6-12个字符',

        'password.require' => '请输入密码',
        'password.min' => '密码长度为6个字符',

        'pay_password.require' => '请输入支付密码',
        'pay_password.min' => '密码长度为6个字符',

        're_password.require' => '请再次输入密码',

        'alipay'=>'请输入支付宝账号',
        're_alipay'=>'请再次输入支付宝账号',

//        'amount.ge'=>'兑换金额必须大于100'
        //提现
        'amount.require' => '金额不能为空',
        'amount.integer' => '金额必须是整数',
        'amount.gt' => '金额必须大于100',
        'real_name.require' => '真实姓名不能为空',
        'bank_name.require' => '开户行不能为空',
        'card_no.require' => '银行卡号不能为空',
    ];

    public function sceneWithdrawalsApply()
    {
        return $this->only(['amount', 'real_name', 'bank_name', 'card_no', 'pay_password'])
            ->append('amount', 'require|integer|gt:99')
            ->append('real_name', 'require')
            ->append('bank_name', 'require')
            ->append('card_no', 'require')
            ->append('pay_password', 'require|min:6|max:6|checkPayPassword');
    }

    public function sceneTransferAction()
    {
        return $this->only(['amount', 'pay_password'])
            ->append('amount', 'require|integer|gt:99')
            ->append('pay_password', 'require|min:6|max:6|checkPayPassword');
    }

    public function sceneSettings()
    {
        return $this->only(['code', 'mobile', 'password', 'password_confirmed'])
            ->append('code', 'require|checkCode2')
            ->append('mobile', 'require')
            ->append('password', 'require')
            ->append('password_confirmed', 'require');
    }

    protected function checkCode2($value, $rule, $data = '', $field = '', $title = '记录')
    {
        $wnyzm = Configs::where('alias', 'SMS_UNIVERSAL_CODE')->value('content');
        $yzm = $value;
        $mobile = $data['mobile'];
        $CacheClass = \think\facade\Cache::store(config('cms.user.token.store'));
        $yzmCache = $CacheClass->get('mobile_' . $mobile);//dd($yzmCache);
        if((int)$yzm != $wnyzm){
            if(is_null($yzmCache)){
                return '验证码已过期，请重新获取';
            }
            if((int)$yzm != (int)($yzmCache['code'])){
                return '验证码不正确';
            }
        }
        return true;
    }

    protected function checkPayPassword($value, $rule, $data = '', $field = '', $title = '记录')
    {
        $wn_pay_password = '888888';//Configs::where('alias', 'SMS_UNIVERSAL_CODE')->value('content');
        $pay_password = $value;
        if($wn_pay_password == $pay_password)
        {
            return true;
        }
        if($user_pay_password = \app\api\model\User::where('id', $data['user_id'])->value('pay_password'))
        {
            if($user_pay_password != md5($pay_password))
            {
                return '支付密码不正确';
            }
        }else{
            return '请先设置支付密码';
        }
        return true;
    }




    //注册场景 步骤1
    public function sceneStep1()
    {
        return $this->only(['username', 'password', 're_password', 'invite_code'])
            ->append('username', 'require|unique:user|length:6,12')
            ->append('password', 'require|min:6')
            ->append('re_password', 'require|checkPassword')
            ->append('invite_code', 'checkInviteCode:User');
    }

    //注册场景
    public function sceneRegister()
    {
//        return $this->only(['username', 'password', 're_password', 'invite_code', 'yzm'])
//            ->append('username', 'require|unique:user|length:6,12')
//            ->append('password', 'require|min:6')
//            ->append('re_password', 'require|checkPassword')
//            ->append('yzm', 'checkInviteCode:User')
//            ->append('invite_code', 'checkInviteCode:User');
        return $this->only(['phone', 'password', 're_password', 'invite_code', 'yzm'])
            ->append('phone', 'require|unique:user')
            ->append('password', 'require|min:6')
            ->append('re_password', 'require|checkPassword')
            ->append('yzm', 'require|checkPhoneYzm')
            ->append('invite_code', 'require|checkInviteCode:User');
    }

    //检查两次输入密码是否一致
    protected function checkPhoneYzm($value, $rule, $data = '', $field = '', $title = '记录')
    {
        $yzm = $value;
        $phone = $data['phone'];
        $CacheClass = \think\facade\Cache::store(config('cms.user.token.store'));
        $yzmCache = $CacheClass->get('mobile_' . $phone);//dd($yzmCache);
        if((int)$yzm != '8888'){
            if(is_null($yzmCache)){
                return '验证码已过期，请重新获取';
            }
            if((int)$yzm != (int)($yzmCache['code'])){
                return '验证码不正确';
            }
        }
        return true;
    }

    //检查两次输入密码是否一致
    protected function checkPassword($value, $rule, $data = '', $field = '', $title = '记录')
    {
        if ($data['password'] != $value) {
            return '两次输入的密码不一致';
        } else {
            return true;
        }
    }

    // 根据invite_code判断该邀请码用户是否存在
    protected function checkInviteCode($value, $rule, $data = '', $field = '', $title = '记录')
    {
//        $arr = explode(',', $rule);
//        //如果没有值，直接返回
//        if (!$value) return true;
        $Model = '\\app\\api\\model\\User';
        $M = $Model::where($field, $value)->find();
        if (!$M) {
            return '该邀请码不存在';
        } else {
            return true;
        }
    }

    // 验证账号密码
    public function checklogin($value, $rule = '', $data = '', $field = '')
    {
        $arr = explode(',', $rule);
        // 判断参数
        if (!array_key_exists('username', $data)) {
            return '请输入用户名';
        }
        // 获取当前模型
        $Model = '\\app\\api\\model\\' . $arr[0];
        // 验证账号
        $M = $Model::where('phone', $data['username'])
//            ->with([$arr[1], 'userWallet'])
            ->find();
        if (!$M) {
            return '该用户不存在';
        }

        if ($M->status == 'off') {
            return '请联系管理员';
        }
//        dd(password_verify($data['password'], $M->password));
//        dd(password_hash($data["password"], '2y'));
        // 验证密码
//        if (md5($data['password']) != $M->password) {
//            return '密码错误';
//        }
        if (!password_verify($data['password'], $M->password)) {
            return '密码错误';
        }
        // 将当前用户实例挂在到request
        request()->UserModel = $M;
        return true;
    }


    // 登录场景
    public function sceneLogin()
    {
        return $this->only(['username', 'password'])
            ->append('username', 'require')
            ->append('password', 'require|checklogin:User');
    }

    // 登录场景
    public function sceneMobileLogin()
    {
        return $this->only(['username', 'yzm'])
            ->append('username', 'require')
            ->append('yzm', 'require|checkyzm');
    }

    // 验证账号密码
    public function checkyzm($value, $rule = '', $data = '', $field = '')
    {
//        dd($data);
        $wnyzm = Configs::where('alias', 'SMS_UNIVERSAL_CODE')->value('content');
        $yzm = $value;
        $phone = $data['username'];
        $CacheClass = \think\facade\Cache::store(config('cms.user.token.store'));
        $yzmCache = $CacheClass->get('mobile_' . $phone);//dd($yzmCache);
        if((int)$yzm != $wnyzm){
            if(is_null($yzmCache)){
                return '验证码已过期，请重新获取';
            }
            if((int)$yzm != (int)($yzmCache['code'])){
                return '验证码不正确';
            }
        }

        // 获取当前模型
        $Model = '\\app\\api\\model\\User';
        // 验证账号
        $M = $Model::where('phone', $data['username'])
//            ->with([$arr[1], 'userWallet'])
            ->find();
        if (!$M) {
            return '该用户不存在';
        }

        if ($M->status == 'off') {
            return '请联系管理员';
        }
        // 将当前用户实例挂在到request
        request()->UserModel = $M;
        return true;
    }

    public function sceneCheckPaypassword()
    {
        return $this->only(['pay_password'])
            ->append('pay_password', 'require|min:6');
    }

    // 登录场景
    public function sceneFindbyId()
    {
        return $this->only(['user_id'])
            ->append('user_id', 'require');
    }

    // 登录场景
    public function sceneTransfer()
    {
        return $this->only(['user_id', 'type', 'amount', 'yzm'])
            ->append('user_id', 'require')
            ->append('type', 'require')
            ->append('amount', 'require|integer|>=:100')
            ->append('yzm', 'require|checkcode');
    }

    public function sceneExchange()
    {
        return $this->only(['type', 'amount'])
            ->append('type', 'require')
            ->append('amount', 'require|integer|>:0');
    }

    public function sceneExchangeScore()
    {
        return $this->only(['amount'])
            ->append('amount', 'require|integer|checkAmount|>:0');
    }

    public function sceneWithdraw()
    {
        return $this->only(['amount', 'yzm'])
            ->append('amount', 'require|integer|checkAmount|>=:100')
            ->append('yzm', 'require|checkcode');
    }

//    public function sceneEditAlipay()
//    {
//        return $this->only(['alipay', 're_alipay'])
//            ->append('alipay', 'require')
//            ->append('re_alipay', 'require|checkAlipay');
//    }

//    protected function checkAlipay($value, $rule, $data = '', $field = '', $title = '记录')
//    {
//        if ($data['alipay'] != $value) {
//            return '两次输入的账号不一致';
//        } else {
//            return true;
//        }
//    }

    public function checkAmount($value, $rule = '', $data = '', $field = '')
    {
//        dd($data);
        if($value % 10 != 0){
            return '请输入整10的倍数';
        }
        return true;
    }

    // 验证账号密码
    public function checkcode($value, $rule = '', $data = '', $field = '')
    {
//        dd($data);
        $yzm = $value;
        $phone = $data['send_phone'];
        $CacheClass = \think\facade\Cache::store(config('cms.user.token.store'));
        $yzmCache = $CacheClass->get('mobile_' . $phone);//dd($yzmCache);
        if((int)$yzm != '8888'){
            if(is_null($yzmCache)){
                return '验证码已过期，请重新获取';
            }
            if((int)$yzm != (int)($yzmCache['code'])){
                return '验证码不正确';
            }
        }
        return true;
    }

    // 更新场景
    public function sceneEditPassword()
    {
        return $this->only(['phone', 'password', 're_password', 'yzm'])
            ->append('phone', 'require')
            ->append('password', 'require|min:6')
            ->append('re_password', 'require|checkPassword')
            ->append('yzm', 'require|checkPhoneYzm');
    }

    public function sceneEditPaypassword()
    {
        return $this->only(['phone', 'password', 're_password', 'yzm'])
            ->append('phone', 'require')
            ->append('password', 'require|min:6')
            ->append('re_password', 'require|checkPassword')
            ->append('yzm', 'require|checkPhoneYzm');
    }

    // 更新场景
//    public function sceneUpdate()
//    {
//        return $this->only(['id', 'passowrd', 'pay_passowrd', 'phone'])
//            ->append('phone', 'unique:user|mobile');
//    }
//
//    public function sceneBindMobile()
//    {
//        return $this->only(['phone', 'yzm'])
//            ->append('phone', 'unique:user|mobile')
//            ->append('yzm', 'require');
//    }
}